/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.Activities;
import me.zhengjie.modules.anniversary.repository.ActivitiesRepository;
import me.zhengjie.modules.anniversary.service.ActivitiesService;
import me.zhengjie.modules.anniversary.service.dto.ActivitiesDto;
import me.zhengjie.modules.anniversary.service.dto.ActivitiesQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.ActivitiesMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class ActivitiesServiceImpl implements ActivitiesService {

    private final ActivitiesRepository activitiesRepository;
    private final ActivitiesMapper activitiesMapper;

    @Override
    public Map<String,Object> queryAll(ActivitiesQueryCriteria criteria, Pageable pageable){
        Page<Activities> page = activitiesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(activitiesMapper::toDto));
    }

    @Override
    public List<ActivitiesDto> queryAll(ActivitiesQueryCriteria criteria){
        return activitiesMapper.toDto(activitiesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActivitiesDto findById(Long id) {
        Activities activities = activitiesRepository.findById(id).orElseGet(Activities::new);
        ValidationUtil.isNull(activities.getId(),"Activities","id",id);
        return activitiesMapper.toDto(activities);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActivitiesDto create(Activities resources) {
        return activitiesMapper.toDto(activitiesRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Activities resources) {
        Activities activities = activitiesRepository.findById(resources.getId()).orElseGet(Activities::new);
        ValidationUtil.isNull( activities.getId(),"Activities","id",resources.getId());
        activities.copy(resources);
        activitiesRepository.save(activities);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            activitiesRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ActivitiesDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ActivitiesDto activities : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", activities.getName());
            map.put("发布人ID", activities.getUserId());
            map.put("内容", activities.getContent());
            map.put("活动开始日期", activities.getStartTime());
            map.put("活动结束日期", activities.getEndTime());
            map.put("预约开始时间", activities.getAppStartTime());
            map.put("预约结束时间", activities.getAppEndTime());
            map.put("创建者名称", activities.getCreateBy());
            map.put("创建时间", activities.getCreateTime());
            map.put("修改者名称", activities.getUpdatedBy());
            map.put("修改时间", activities.getUpdateTime());
            map.put("排序", activities.getSort());
            map.put("备注", activities.getRemark());
            map.put("是否启用", activities.getIsEnable());
            map.put("审核状态（0：待审核、1：审核通过、2：审核未通过）", activities.getVerifyStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}